﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections.Concurrent;
using System.Threading;
using System.ServiceModel;
using CRM.Index.Box;
using CRM.Index.IProvider;
using CRM.Entity;
using CRM.Entity.Extra;
using CRM.Entity.Page;

namespace CRM.Index.Provider
{
   
    public partial class ProviderService : IProviderService
    {
       public static ReaderWriterLockSlim  textInfo_rws = new ReaderWriterLockSlim();
       
        #region 插入操作
        /// <summary>
        /// 插入操作
        /// </summary>
        /// <param name="TextInfo"></param>
        public bool TextInfo_Insert(TextInfo textInfo)
        {
          try
            {
                textInfo_rws.EnterWriteLock();
                
                DictionaryBox.textInfoDictionary.Add(textInfo.TextId, textInfo);

                DictionaryBox.textInfoTextIdSort.Add(textInfo.TextId);
                
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                
                return false;
            }
            finally
            {
                textInfo_rws.ExitWriteLock();
            }
        }
        #endregion

        #region 更新操作
        /// <summary>
        /// 更新操作
        /// </summary>
        /// <param name="TextInfo"></param>
        public bool TextInfo_Update(TextInfo textInfo)
        {
          try
            {
                textInfo_rws.EnterWriteLock();

                DictionaryBox.textInfoDictionary[textInfo.TextId] = textInfo;
                
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                
                return false;
            }
            finally
            {
                textInfo_rws.ExitWriteLock();
            }
        }
        #endregion

        #region 删除操作
        /// <summary>
        /// 删除操作
        /// </summary>
        /// <param name="TextInfo"></param>
        public bool TextInfo_Delete(TextInfo textInfo)
        {
          try
            {
                textInfo_rws.EnterWriteLock();

                DictionaryBox.textInfoDictionary.Remove(textInfo.TextId);

                DictionaryBox.textInfoTextIdSort.Remove(textInfo.TextId);
                
                return true;
            }  
            catch (Exception e)
            {
                Console.WriteLine(e.Message);

                return false;
            }
            finally
            {
                textInfo_rws.ExitWriteLock();
            }
        }
        #endregion
        
        #region 根据主键获取一条
        /// <summary>
        /// 根据主键获取一条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TextInfo TextInfo_Single(Int64 id)
        {
            try
            {
                textInfo_rws.EnterReadLock();

                return DictionaryBox.textInfoDictionary[id];
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                textInfo_rws.ExitReadLock();
            }
        }
        #endregion
        
        #region 获取TopK条
        /// <summary>
        /// 获取TopK条
        /// </summary>
        /// <param name="top"></param>
        public TextInfoPage TextInfo_Top(int top)
        {
            try
            {
                 textInfo_rws.EnterReadLock();

                var  textInfoPage = new  TextInfoPage(){ ListEntity = new List<TextInfo>() };

                var skip = DictionaryBox.textInfoTextIdSort.Count - top < 0 ? 0 : DictionaryBox.textInfoTextIdSort.Count - top;

                var listID = DictionaryBox.textInfoTextIdSort.Skip(skip).Take(top).ToList();

                for (int i = listID.Count - 1; i >= 0; i--)
                {
                    textInfoPage.ListEntity.Add(DictionaryBox.textInfoDictionary[listID[i]]);
                }

                textInfoPage.RecordCount = DictionaryBox.textInfoTextIdSort.Count;

                return textInfoPage;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                 textInfo_rws.ExitReadLock();
            }
        }
        #endregion

        #region 分页获取条数
        /// <summary>
        /// 分页获取条数
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public TextInfoPage TextInfo_Page(PageInfo info)
        {
            try
            {
                textInfo_rws.EnterReadLock();

                var textInfoPage = new TextInfoPage() { ListEntity = new List<TextInfo>() };

                var skip = DictionaryBox.textInfoTextIdSort.Count - (info.PageIndex * info.PageSize);

                info.PageSize = skip < 0 ? skip + info.PageSize : info.PageSize;

                var listID = DictionaryBox.textInfoTextIdSort.Skip(skip).Take(info.PageSize).ToList();

                for (int i = listID.Count - 1; i >= 0; i--)
                {
                    textInfoPage.ListEntity.Add(DictionaryBox.textInfoDictionary[listID[i]]);
                }

                textInfoPage.RecordCount = DictionaryBox.textInfoTextIdSort.Count;

                return textInfoPage;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                textInfo_rws.ExitReadLock();
            }
        } 
        #endregion
        
        #region 分页获取条数
        /// <summary>
        /// 分页获取条数
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public TextInfoExtraPage TextInfoExtra_Page(PageInfo info)
        {
            try
            {
                textInfo_rws.EnterReadLock();

                var textInfoExtaPage = new TextInfoExtraPage() { ListEntity = new List<TextInfoExtra>() };

                var skip = DictionaryBox.textInfoTextIdSort.Count - (info.PageIndex * info.PageSize);

                info.PageSize = skip < 0 ? skip + info.PageSize : info.PageSize;

                var listID = DictionaryBox.textInfoTextIdSort.Skip(skip).Take(info.PageSize).ToList();

                //for (int i = listID.Count - 1; i >= 0; i--)
                for (int i = 0; i < listID.Count; i++)
                {
                    var single = DictionaryBox.textInfoDictionary[listID[i]];

                    var textInfoExtra = new TextInfoExtra()
                    {
                        TextId = single.TextId,
                        TextKey = single.TextKey,
                        TextValue = single.TextValue,
                    };

                    textInfoExtaPage.ListEntity.Add(textInfoExtra);
                }

                textInfoExtaPage.RecordCount = DictionaryBox.textInfoTextIdSort.Count;

                return textInfoExtaPage;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                textInfo_rws.ExitReadLock();
            }
        }
        #endregion
    }
}
